<template>
  <div>
    <!-- 选择框 -->
    <el-card style="margin-bottom:20px">
      <CategoySelector @changeCategoryId='changeCategoryId' />
    </el-card>

    <el-card>
      <div v-show="!isShowSpuForm&&!isShowSkuForm">
        <el-button type="primary" icon="el-icon-plus" style="margin-bottom:20px" :disabled='!category3Id'
          @click="showAddSpuForm">
          添加SPU
        </el-button>

        <!-- 平台属性 -->
        <el-table :data="spuList" border stripe style="width: 100%;">
          <el-table-column type="index" label="序号" width="100" align='center'>
          </el-table-column>
          <el-table-column label="SPU名称" width="150" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="操作" width="250">
            <template slot-scope="{row}">
              <HintButton title="添加SKU" size="mini" type="primary" icon="el-icon-plus" @click="showAddSkuForm(row)" />
              <HintButton title="修改SPU" size="mini" type="primary" icon="el-icon-edit"
                @click="showUpdateSpuForm(row.id)" />
              <HintButton title="查看SKU" size="mini" type="info" icon="el-icon-info" @click="showSkuInfoList(row)" />
              <HintButton title="删除SPU" size="mini" type="danger" icon="el-icon-delete" @click="deleteSpu(row.id)" />
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <el-pagination @size-change="handleSizeChange" @current-change="getSpuList" :current-page="page"
          :page-sizes="[2, 4, 6]" :page-size="limit" layout=" prev, pager, next, jumper, ->, sizes,total" :total="total"
          style="text-align:center;" v-show="category3Id">
        </el-pagination>
      </div>

      <SpuForm :visible.sync='isShowSpuForm' ref="spuForm" @saveSuccess='saveSuccess' />

      <SkuForm v-show="isShowSkuForm" ref="skuForm" @saveSuccess="saveSuccess" @cancel="isShowSkuForm=false" />
    </el-card>

    <!-- 对话框，展示spu对象对应的sku的列表数据 -->
    <el-dialog :title="spuName" :visible.sync="dialogTableVisible">
      <el-table :data="skuInfoList">
        <el-table-column property="skuName" label="名称" width="150"></el-table-column>
        <el-table-column property="price" label="价格(元)" width="200"></el-table-column>
        <el-table-column property="weight" label="重量(kg)"></el-table-column>
        <el-table-column label="默认图片">
          <template slot-scope="{row}">
            <img :src="row.skuDefaultImg" height="100">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
  import SpuForm from '../components/spuForm';
  import SkuForm from '../components/skuForm';

  export default {
    name: 'Spu',
    components: {
      SpuForm,
      SkuForm
    },
    data() {
      return {
        category1Id: '', //一级分类id
        category2Id: '', //二级分类id
        category3Id: '', //三级分类id
        spuList: [], //平台属性数组
        limit: 1, //每页展示的数量
        page: 1, //每页展示的条数
        total: 0, //展示总数量
        isShowSpuForm: false, //控制form界面的展示
        spuId: 0,
        isShowSkuForm: false, // 是否显示skuForm
        dialogTableVisible: false, // 控制SKU信息对话框
        spuName: '', // 用来存储spu名字
        skuInfoList: [] //sku的列表数据
      }
    },
    methods: {
      //三级导航改变时触发
      changeCategoryId({
        level,
        categoryId
      }) {
        switch (level) {
          case 1:
            this.category1Id = categoryId;
            //重置二级三级分类的id和平台属性
            this.category2Id = null;
            this.category3Id = null;
            this.spuList = [];
            break
          case 2:
            this.category2Id = categoryId;
            this.category3Id = null;
            this.spuList = [];
            break
          case 3:
            this.category3Id = categoryId;
            this.spuList = [];
            this.loding = true;
            this.getSpuList();
            break
        }
      },
      //获取spu数据
      async getSpuList(page = 1) {
        this.page = page; //更新页码
        let result = await this.$API.spu.getSpuInfoList(this.page, this.limit, this.category3Id);
        if (result.code !== 200) { //判断是否获取数据成功
          this.$message.error('获取品牌信息失败'); //弹出错误信息
          return;
        }
        //更新数据
        const {
          records,
          total
        } = result.data;
        this.spuList = records;
        this.total = total;
      },
      //分页器---每页展示多少条
      handleSizeChange(val) {
        this.limit = val;
        this.getSpuList();
      },
      //修改
      showUpdateSpuForm(id) {
        this.spuId = id; //保存当前修改的3id
        this.$refs.spuForm.initUpdateData(id); //把3id传递给自组件
        this.isShowSpuForm = true; //修改界面显示
      },
      // 添加或修改成功
      saveSuccess() {
        // 有id说明是修改操作,从当前页刷新
        if (this.spuId) {
          this.getSpuInfoList(this.page);
          this.spuId = '';
        } else {
          this.getSpuList(); // 此为添加数据
        }
      },
      //删除
      async deleteSpu(id) {
        try {
          let result = await this.$API.spu.deleteSpuInfoById(id);
          this.$message.success('操作成功');
          this.getSpuList(this.page > 1 ? (this.spuList.length === 1 ? this.page - 1 : this.page) : 1);
        } catch (error) {
          this.$message.error(error.message || '操作失败');
        }
      },
      //添加spuInfo对象
      showAddSpuForm() {
        this.isShowSpuForm = true; // 展示添加页
        this.$refs.spuForm.initAddData(this.category3Id);
      },
      // 点击添加Sku按钮,对应的回调函数
      showAddSkuForm(spuInfo) {
        this.isShowSkuForm = true; // 展示添加sku页
        // 获取spuId,spuName,tmId,一级/二级/三级分类的id
        spuInfo = {
          ...spuInfo,
          category1Id: this.category1Id,
          category2Id: this.category2Id
        }
        // 调用skuForm组件中相关的方法进行数据初始化
        this.$refs.skuForm.initAddData(spuInfo);
      },
      // 展示SKU信息
      async showSkuInfoList(spuInfo) {
        this.spuName = spuInfo.spuName;
        this.dialogTableVisible = true;
        const result = await this.$API.sku.getSkuInfoListBySpuId(spuInfo.id);
        this.skuInfoList = result.data;
      }
    }
  }

</script>

<style>

</style>
